class Node:
    def __init__(self, val):
        self.val = val
        self.neighbors = {}
        self.flag = 0


class Solution(object):
    def removeSubfolders(self, folder):
        folder = [f.split('/')[1:] for f in folder]
        folder.sort(key=lambda x: len(x))
        root = Node('#')
        out = []
        for f in folder:
            node = root
            for w in f:
                if w not in node.neighbors:
                    new_node = Node(w)
                    node.neighbors[w] = new_node
                    node = new_node
                else:
                    node = node.neighbors[w]
                    if node.flag:
                        break
            if not node.flag:
                out.append('/' + '/'.join(f))
            node.flag = 1
        return out


